package fr.afcepf.atod19.dreamproject.service.implementation;

import fr.afcepf.atod19.dreamproject.dto.ClientDTO;
import fr.afcepf.atod19.dreamproject.dtoutils.EntityToDTOConverter;
import fr.afcepf.atod19.dreamproject.entity.Client;
import fr.afcepf.atod19.dreamproject.service.interfaces.IConnexionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/connexion")
public class ConnexionServiceImpl implements IConnexionService {

    static Logger logger = LoggerFactory.getLogger(ConnexionServiceImpl.class);

    private Client client;

    public static Logger getLogger() {
        return logger;
    }

    public static void setLogger(Logger logger) {
        ConnexionServiceImpl.logger = logger;
    }

    @Path("/getClient")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ClientDTO getClient() {
        ClientDTO clientDTO = EntityToDTOConverter.getClientDTOWithAdresses(client);
        return clientDTO;
    }

    private fr.afcepf.atod19.dreamproject.business.interfaces.IBusinessClient businessClient;

    @Path("/authentification/{login}/{password}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ClientDTO authentification(@PathParam("login") String login,
                                      @PathParam("password") String password) {
        this.client = businessClient.connexion(login, password);
        ClientDTO clientDTO = EntityToDTOConverter.getClientDTOWithAdresses(client);
        return clientDTO;
    }

    @Path("/logout")
    @GET
    @Produces("application/json")
    public ClientDTO logout() {
        this.client = null;
        return new ClientDTO();
    }

    public fr.afcepf.atod19.dreamproject.business.interfaces.IBusinessClient getBusinessClient() {
        return businessClient;
    }

    public void setBusinessClient(
            fr.afcepf.atod19.dreamproject.business.interfaces.IBusinessClient businessClient) {
        this.businessClient = businessClient;
    }

}
